package com.sl.mes.app.service.mm;

import com.sl.mes.app.dao.mm.AggMmPickmVo;
import com.sl.mes.app.dao.mm.AggMoVo;
import com.sl.mes.db.itf.mm.IMmPickm;
import com.sl.mes.db.vo.MmMo;
import com.sl.mes.db.vo.MmPickm;
import com.sl.mes.db.vo.MmPickmB;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class MmPickmService {

    @Resource
    private IMmPickm iMmPickmService;


    /**
     * 根据id进行查询
     * @param cpickmid
     * @return
     */
    public AggMmPickmVo getAggMmPickmVoById(String cpickmid){
        AggMmPickmVo aggMmPickmVo = new AggMmPickmVo();
        MmPickm mmPickm = iMmPickmService.findParentVoById(cpickmid);
        List<MmPickmB> lstMmpickmB = iMmPickmService.findChildrenVoById(cpickmid);
        aggMmPickmVo.setMmPickm(mmPickm);
        aggMmPickmVo.setLstMmpickmB(lstMmpickmB);
        return aggMmPickmVo;
    }

    /**
     * 查询备料计划
     * @param
     * @return
     */
    public AggMmPickmVo getAggMmPickmVoByCfirstbillrowid(String cfirstbillrowid){
        AggMmPickmVo aggMmPickmVo = new AggMmPickmVo();
        MmPickm mmPickm = iMmPickmService.findParentVoByCfirstbillrowid(cfirstbillrowid);
        List<MmPickmB> lstMmpickmB = iMmPickmService.findChildrenVoByCpickmid(mmPickm.getCpickmid());
        aggMmPickmVo.setMmPickm(mmPickm);
        aggMmPickmVo.setLstMmpickmB(lstMmpickmB);
        return aggMmPickmVo;
    }

    /**
     * 根据id进行删除
     * @return
     */
    public int  deleteAggMmPcikmVoById(String cpickmid){
        iMmPickmService.deleteParentVoById(cpickmid);
        int reg  =  iMmPickmService.deleteChildrenVoById(cpickmid);
        return reg;
    }

    /**
     * 添加备料计划
     * @param aggMmPickmVo
     * @return
     */
    public int  addAggMmPcikmVo(AggMmPickmVo aggMmPickmVo){
        int res=0;
         MmPickm mmPickm = aggMmPickmVo.getMmPickm();
         res = iMmPickmService.addParentVo(mmPickm);
         List<MmPickmB> lstMmPickmB = aggMmPickmVo.getLstMmpickmB();
         for (MmPickmB mmPickmB:lstMmPickmB){
             iMmPickmService.addChildrenVo(mmPickmB);
         }
         return res;
    }

    /**
     * 修改备料计划
     *
     * @param aggMmPickmVo
     */
    public boolean updataMmPickm(AggMmPickmVo aggMmPickmVo) {


        boolean res = true;
        try {
            if (aggMmPickmVo != null) {
                iMmPickmService.updateMmPickm(aggMmPickmVo.getMmPickm());
                List<MmPickmB> mmPickmBList = aggMmPickmVo.getLstMmpickmB();
                for (int i = 0; i < mmPickmBList.size(); i++) {
                    MmPickmB mmPickmB = mmPickmBList.get(i);
                    mmPickmB.setTs(LocalDateTime.now());
                    iMmPickmService.updateMmPickmB(mmPickmB);
                }
            }
        } catch (Exception e) {
            res = false;
        }
        return res;
    }

}
